package proeva;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Report_Game_Servlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Report_Game_Servlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

//		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String json;
		//有传入参数WorD,来判断需要输出的是蠕虫博弈还是DDoS博弈
		boolean isWorm = true;
		if( request.getParameter("WorD").equalsIgnoreCase("d"))
			isWorm = false;
		if( isWorm )//蠕虫
		{
			try
			{
				Connection Conn = DBSetting.GetGameConnection();
				Statement stmt = Conn.createStatement();
				ResultSet result = stmt.executeQuery("SELECT * FROM Game WHERE WorD='true' ORDER BY Time DESC");
				StringBuffer data = new StringBuffer("");
				while( result.next() )
				{
					data.append("{Att_Measure:");
					data.append(result.getString("Att_Measure"));

					data.append(",Def_Measure:");
					data.append(result.getString("Def_Measure"));

					data.append(",Time:");
					data.append(result.getString("Time"));

					data.append(",Result:");
					data.append(result.getString("Result"));

					data.append("},");
				}
				data.deleteCharAt(data.length() - 1);
				json = "{success:true,msg:'查询数据库成功',data:{"+ data +"}}";
				out.write(json);

				result.close();
				stmt.close();
				Conn.close();
			}
			catch( Exception ex )
			{
				json = "{success:false,msg:'查询数据库时出错，请重试',data:{}}";
				out.write(json);
				out.write( ex.getMessage() );
				return;
			}
		}
		else//DDoS
		{
			try
			{
				Connection Conn = DBSetting.GetGameConnection();
				Statement stmt = Conn.createStatement();
				ResultSet result = stmt.executeQuery("SELECT * FROM Game WHERE WorD='false' ORDER BY Time DESC");
				StringBuffer data = new StringBuffer("");
				while( result.next() )
				{
					data.append("{Att_Measure:");
					data.append(result.getString("Att_Measure"));

					data.append(",Def_Measure:");
					data.append(result.getString("Def_Measure"));

					data.append(",Time:");
					data.append(result.getString("Time"));

					data.append(",Result:");
					data.append(result.getString("Result"));

					data.append("},");
				}
				data.deleteCharAt(data.length() - 1);
				json = "{success:true,msg:'查询数据库成功',data:{"+ data +"}}";
				out.write(json);

				result.close();
				stmt.close();
				Conn.close();
			}
			catch( Exception ex )
			{
				json = "{success:false,msg:'查询数据库时出错，请重试',data:{}}";
				out.write(json);
				out.write( ex.getMessage() );
				return;
			}
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet( request, response );
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
